'
' *  Authors:  Benton Stark
' * 
' *  Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com) 
' *
' * Permission is hereby granted, free of charge, to any person obtaining a copy
' * of this software and associated documentation files (the "Software"), to deal
' * in the Software without restriction, including without limitation the rights
' * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' * copies of the Software, and to permit persons to whom the Software is
' * furnished to do so, subject to the following conditions:
' * 
' * The above copyright notice and this permission notice shall be included in
' * all copies or substantial portions of the Software.
' * 
' * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' * THE SOFTWARE.
' * 
' 


Imports System.Text

Namespace FTP
    ''' <summary>
    ''' FTP utility methods.
    ''' </summary>
    Public Class FtpUtilities
        Private Const MODE_VAL_MIN_VALUE As Integer = 0
        Private Const MODE_VAL_MAX_VALUE As Integer = 777

        Private Const MODE_STR_MIN_LEN As Integer = 9
        Private Const MODE_STR_MAX_LEN As Integer = 10

        Private Const MODE_STR_NONE As String = "---"
        Private Const MODE_STR___x As String = "--x"
        Private Const MODE_STR__w_ As String = "-w-"
        Private Const MODE_STR__wx As String = "-wx"
        Private Const MODE_STR_r__ As String = "r--"
        Private Const MODE_STR_r_x As String = "r-x"
        Private Const MODE_STR_rw_ As String = "rw-"
        Private Const MODE_STR_rwx As String = "rwx"

        Private Const MODE_CHAR_NONE As Char = "0"c
        Private Const MODE_CHAR___x As Char = "1"c
        Private Const MODE_CHAR__w_ As Char = "2"c
        Private Const MODE_CHAR__wx As Char = "3"c
        Private Const MODE_CHAR_r__ As Char = "4"c
        Private Const MODE_CHAR_r_x As Char = "5"c
        Private Const MODE_CHAR_rw_ As Char = "6"c
        Private Const MODE_CHAR_rwx As Char = "7"c

        Private Const MODE_INT_NONE As Integer = 0
        Private Const MODE_INT___x As Integer = 1
        Private Const MODE_INT__w_ As Integer = 2
        Private Const MODE_INT__wx As Integer = 3
        Private Const MODE_INT_r__ As Integer = 4
        Private Const MODE_INT_r_x As Integer = 5
        Private Const MODE_INT_rw_ As Integer = 6
        Private Const MODE_INT_rwx As Integer = 7

        ''' <summary>
        ''' Converts the UNIX 3 digit mode value to UNIX attribute string.
        ''' </summary>
        ''' <param name="mode">Three digit mode value.</param>
        ''' <return>Attribute string.</return>
        Public Shared Function ModeToAttribute(mode As Integer) As String
            ' check that the mode value is within the correct size limits
            If mode < MODE_VAL_MIN_VALUE OrElse mode > MODE_VAL_MAX_VALUE Then
                Return [String].Empty
            End If

            Dim s As String = mode.ToString()
            Dim ary As Char() = s.ToCharArray()
            Dim b As New StringBuilder()

            For Each c As Char In ary
                Select Case c
                    Case MODE_CHAR_NONE
                        b.Append(MODE_STR_NONE)
                        Exit Select
                    Case MODE_CHAR___x
                        b.Append(MODE_STR___x)
                        Exit Select
                    Case MODE_CHAR__w_
                        b.Append(MODE_CHAR__w_)
                        Exit Select
                    Case MODE_CHAR__wx
                        b.Append(MODE_STR__wx)
                        Exit Select
                    Case MODE_CHAR_r__
                        b.Append(MODE_STR_r__)
                        Exit Select
                    Case MODE_CHAR_r_x
                        b.Append(MODE_STR_r_x)
                        Exit Select
                    Case MODE_CHAR_rw_
                        b.Append(MODE_STR_rw_)
                        Exit Select
                    Case MODE_CHAR_rwx
                        b.Append(MODE_STR_rwx)
                        Exit Select
                    Case Else
                        b.Append(MODE_STR_NONE)
                        Exit Select
                End Select
            Next
            Return b.ToString()
        End Function

        ''' <summary>
        ''' Convert UNIX attribute to UNIX three digit mode value.
        ''' </summary>
        ''' <param name="s">Attribute string.</param>
        ''' <return>Three digit mode integer value.</return>
        Public Shared Function AttributeToMode(s As String) As Integer
            If s.Length < MODE_STR_MIN_LEN OrElse s.Length > MODE_STR_MAX_LEN Then
                Return 0
            End If

            ' strip off the leading unix attribute 
            If s.Length = MODE_STR_MAX_LEN Then
                s = s.Substring(1)
            End If

            Dim user As String = s.Substring(0, 3)
            Dim group As String = s.Substring(3, 3)
            Dim other As String = s.Substring(6, 3)

            Dim mode As Integer = ParseAttrib(user) * 100
            mode += ParseAttrib(group) * 10
            mode += ParseAttrib(other)
            Return mode
        End Function

        Private Shared Function ParseAttrib(a As String) As Integer
            Select Case a
                Case MODE_STR_NONE
                    Return MODE_INT_NONE
                Case MODE_STR___x
                    Return MODE_INT___x
                Case MODE_STR__w_
                    Return MODE_INT__w_
                Case MODE_STR__wx
                    Return MODE_INT__wx
                Case MODE_STR_r__
                    Return MODE_INT_r__
                Case MODE_STR_r_x
                    Return MODE_INT_r_x
                Case MODE_STR_rw_
                    Return MODE_INT_rw_
                Case MODE_STR_rwx
                    Return MODE_INT_rwx
                Case Else
                    Return MODE_INT_NONE
            End Select

        End Function

    End Class
End Namespace
